/*
 * 文件名： BaseActivity.java
 * 描    述：Activity基类
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.miguo.app.AppHelper;
import com.miguo.framework.activity.CommonActivity;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public abstract class BaseActivity extends CommonActivity {

    protected boolean isBackBtn = true;// 判断BackBtn 是否可用 当isBackBtn为false返回Main页

    protected boolean isBack = true; // 判断onBackPressed 是否可用 当isBack为false返回Main页

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initExtra();
    }

    private void initExtra() {
        Intent intent = getIntent();
        isBack = intent.getBooleanExtra("back", true);
        isBackBtn = intent.getBooleanExtra("back_btn", true);
        if(isBackBtn == false){
            isBack = false;
            isBackBtn = false;
        }
        if (intent.hasExtra("status")) {
            if (intent.getStringExtra("status").equals("1")) {
                isBack = false;
                isBackBtn = false;
            }
        }
        if (isBackBtn == false || isBack == false) {
            setSwipeBackEnable(false);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isBack == false) {
            AppHelper.StartMain(this, "0");
        }
        overridePendingTransition(0, R.anim.push_right_out);
        this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return false;
    }

    @Override
    public void onLowMemory() {
        ImageLoader.getInstance().clearMemoryCache();
        super.onLowMemory();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Statistics.onResume(this);
        ImageLoader.getInstance().resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Statistics.onPause(this);
        ImageLoader.getInstance().stop();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Statistics.onStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}